import http from "./index.js"
/* 用户登录 */
const Userlogin=(username,password)=>{
    return new Promise((resolve,reject)=>{
        http.post("/api/user/login",{username,password}).then((res)=>{
            resolve(res)
        })
    });
}
/* 判断是否登录 */
const isLogin = ()=>{
    return new Promise((resolve,reject)=>{
        http.get("/api/user/islogin").then((res)=>{
            resolve(res)
        })
    })
}
/* 获取路由的列表 */
const getRouterList =()=>{
    return new Promise((resolve,reject)=>{
        http.post("/api/user/getAuthByuserid").then(res=>{
            resolve(res)
        })
    })
}
let promise
/* 刷新token */
const refreshToken = (headers)=>{
    if(promise){
        return promise
    }
    promise=new Promise((resolve,reject)=>{
        http.get("/api/user/refreshtoken",{headers}
        ).then((res)=>{
            resolve(res)
        })
    })
    promise.finally(()=>{
        promise=null
    })
    return promise
}
/* 获取七牛云的token */
const getQiniuToken = ()=>{
    return new Promise((resolve,reject)=>{
        http.get("/api/user/getqiniutoken").then((res)=>{
            resolve(res)
        })
    })
}
/* 上传七牛云 */
const uploadtoqiniu =(formdata,config)=>{
    return new Promise((resolve,reject)=>{
        http.post("https://upload-z1.qiniup.com",formdata,config).then((res)=>{
            resolve(res)
        })
    })
}
const getuser = ()=>{
    return new Promise((resolve, reject) => {
        http.get("/api/user/getuser").then((res) => {
            resolve(res)
        })
    })
}
/* 修改用户信息 */
const updateuser =(data)=>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/updateuser",data).then((res) => {
            resolve(res)
        })
    })
}
/* 修改密码 */
const changepassword = (data)=>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/changepassword",data).then((res) => {
            resolve(res)
        })
    })
}
const registerusr = (data) =>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/registerusr",data).then(res=>{
            resolve(res)
        })
    })
}
/* 首页数据 */
const getdata = ()=>{
    return new Promise((resolve, reject) => {
        http.get("/api/user/getdata").then(res=>{
            resolve(res)
        })
    })
}
/*  */
const getallusers = (data)=>{
    return new Promise((resolve, reject) => {
        http.get("/api/user/getallusers",{
            params:data
        }).then(res=>{
            resolve(res)
        })
    })
}
/* 获取用户数量 */
const getusernum = ()=>{
    return new Promise((resolve, reject) => {
        http.get("/api/user/getusernum").then(res=>{
            resolve(res)
        })
    })
}
/* 删除用户 */
const deleteusers = (data)=>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/deleteusers",data).then(res=>{
            resolve(res)
        })
    })
}
/* 批量删除用户头像 */
const deleteuserscorver = (data)=>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/deleteuserscorver",data).then(res=>{
            resolve(res)
        })
    })
}
const tochangerole = (data)=>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/tochangerole",data).then(res=>{
            resolve(res)
        })
    })
}
const isuser = (data)=>{
    return new Promise((resolve, reject) => {
        http.post("/api/user/isuser",data).then(res=>{
            resolve(res)
        })
    })
}
const canuser = (data)=>{
    return new Promise((resolve,reject)=>{
        http.post("/api/user/canuser",data).then((res)=>{
            resolve(res)
        })
    })
}
export  {
    canuser,
    Userlogin,
    isLogin,
    refreshToken,
    getRouterList,
    getQiniuToken,
    uploadtoqiniu,
    getuser,
    updateuser,
    changepassword,
    registerusr,
    getdata,
    getallusers,
    getusernum,
    deleteusers,
    deleteuserscorver,
    tochangerole,
    isuser
}